//
//  ActDetailViewController.m
//  TravelFirst
//
//  Created by lanou3g on 15/9/19.
//  Copyright (c) 2015年 吴娅君. All rights reserved.
//

#import "ActDetailViewController.h"
#import "ActDetailView.h"
#import "LoginViewController.h"
#import "ActDetailModel.h"

#import "GTMBase64.h"

@interface ActDetailViewController ()

@property (nonatomic,strong) ActDetailModel *model;
@property (nonatomic) CGFloat detailDate;
@property (nonatomic) NSMutableArray *array;
@property (nonatomic) NSString *aa;
@end


@implementation ActDetailViewController


//解析详情页数据
- (void)jsonWithDetail{
    
    [LORequestManger GET:_detailUrl success:^(id response) {
        NSDictionary *mdic = (NSDictionary *)response;
        self.model = [[ActDetailModel alloc] init];
        [_model setValuesForKeysWithDictionary:mdic];
        [self Assignment];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
    }];

}

//将base64  转换成 nsstring 的字符串  需要导入GTMBase64.h
- (NSString *)setBase64:(NSString *)originStr{
    NSData* originData = [originStr dataUsingEncoding:NSUTF8StringEncoding];
    originData = [GTMBase64 decodeData:originData];
    NSString * a=[[NSString alloc] initWithData:originData encoding:NSUTF8StringEncoding];
    return a;
}

//自适应高度
- (CGFloat)lableHeight:(NSString *)str Width:(CGFloat)width font:(CGFloat)font{
    CGRect labelsize = [str boundingRectWithSize:CGSizeMake(width,2000) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:font] forKey:NSFontAttributeName] context:nil];
    return labelsize.size.height;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //隐藏TabBar
    self.navigationController.tabBarController.tabBar.alpha = 0;
    
    [self jsonWithDetail];
    //让导航控制条
    self.navigationController.navigationBar.translucent = NO;
    
}

//赋值操作
- (void)Assignment{
    CGFloat title = [self lableHeight: [self setBase64:_model.title] Width:kWidth - 20 font:20];
    CGFloat address = [self lableHeight: [self setBase64:_model.gather_place] Width:200 font:17];
    NSString *acttext = [[self setBase64:_model.attraction] stringByReplacingOccurrencesOfString:@"<br />" withString:@"\n"];//替换字符串
    CGFloat actLable = [self lableHeight:acttext Width:kWidth-20 font:14];
    
    NSMutableArray *arr = [NSMutableArray array];
    NSMutableArray *sparr = [NSMutableArray array];
    NSNumber *last=[_model.routes lastObject][@"start_date"];
    NSInteger temp = [last intValue];
    CGFloat total=0;
    for(int i = 0;i <= temp/86400; i++){
        CGFloat detailDate=0;
        NSMutableArray *array = [NSMutableArray array];
        for (NSDictionary *val in _model.routes) {
            if([val[@"start_date"] intValue] == i*86400){
                NSString *aa = [self setBase64:val[@"content"]];
                aa=[aa stringByReplacingOccurrencesOfString:@"<br />" withString:@"\n"];
                detailDate += [self lableHeight:aa Width:kWidth - 120 font:14];
                detailDate += 10;
                NSMutableDictionary *d=[NSMutableDictionary dictionaryWithDictionary:val];
                
                NSNumber *num=[NSNumber numberWithFloat:[self lableHeight:aa Width:kWidth - 120 font:14]];
                [d setObject:num forKey:@"height"];
                [d setValue:aa forKey:@"content"];
                [array addObject:d];
            }
        }
        NSNumber *s=[NSNumber numberWithFloat:detailDate];
        [sparr addObject:s];
        total+=detailDate;
        total+=30;
        [arr addObject:array];
    }
   
    
    CGFloat requirement = [self lableHeight: [[self setBase64:_model.equipment]stringByReplacingOccurrencesOfString:@"<br />" withString:@"\n"] Width:kWidth - 20 font:14];
    CGFloat moneyDetail1 = [self lableHeight: [[self setBase64:_model.cost_detail] stringByReplacingOccurrencesOfString:@"<br />" withString:@"\n"]Width:kWidth - 20 font:14];

    CGFloat knowDetail = [self lableHeight: [[self setBase64:_model.application_notes] stringByReplacingOccurrencesOfString:@"<br />" withString:@"\n"] Width:kWidth - 20 font:14];
    
    NSDictionary *dic = [_model.routes lastObject];

    ActDetailView *view = [[ActDetailView alloc] initWithFrame:CGRectMake(0,0, kWidth, kHeight) target:self action:@selector(TapPic) heightTitle:title address:address actLable:actLable detailDate:total fen:sparr requirement:requirement moneyDetail1:moneyDetail1 knowDetail:knowDetail date:dic[@"start_date"] arr:_model.pictures detailArr:arr];
    view.backgroundColor = [UIColor whiteColor];
    view.title.text = [self setBase64:_model.title];
    [view.logo sd_setImageWithURL:[NSURL URLWithString:_model.club[@"logo"]]];
    view.logoTitle.text = _model.club[@"title"];
    for(int i = 0;i < _model.pictures.count;i++){
        
        TapImageView *tapp = (TapImageView *)[view viewWithTag:200+i];
        [tapp sd_setImageWithURL:[NSURL URLWithString:_model.pictures[i][@"picture"]]];
    }
    view.ActTime.text = [self Date:_model.start_time];
    view.ActDay.text = [NSString stringWithFormat:@"%ld天",_day];
    view.ActAddress.text = [self setBase64:_model.destination ];
    view.ActType.text = [_model.tag componentsJoinedByString:@","];//把数组里的元素按,的方式连接
    if([_model.stay_place isEqualToString:@""]){
        view.HotelType.text = @"无";
    }else{
        view.HotelType.text = [self setBase64:_model.stay_place];
    }
    
    if(_model.min_cost == _model.max_cost){
        view.ActCost.text = [NSString stringWithFormat:@"%@元/人",_model.max_cost];
    }else{
        view.ActCost.text = [NSString stringWithFormat:@"%@~%@元/人",_model.min_cost,_model.max_cost];
    }
    
    view.ActPlace.text = [self setBase64:_model.gather_place ];
    view.ActLable.text = acttext;
    
    
    
    view.Requirement.text = [[self setBase64:_model.equipment] stringByReplacingOccurrencesOfString:@"<br />" withString:@"\n"];
    view.knowDetail.text = [[self setBase64:_model.application_notes]stringByReplacingOccurrencesOfString:@"<br />" withString:@"\n"];
    view.moneyDetail1.text = [[self setBase64:_model.cost_detail]stringByReplacingOccurrencesOfString:@"<br />" withString:@"\n"];
    
    //通过tag值获取到收藏的 某个btn 并添加点击事件
    UIButton *butt = (UIButton *)[view viewWithTag:600];
    [butt addTarget:self action:@selector(phone:) forControlEvents:(UIControlEventTouchUpInside)];
    
    UIButton *butt2 = (UIButton *)[view viewWithTag:601];
    [butt2 addTarget:self action:@selector(Collection:) forControlEvents:(UIControlEventTouchUpInside)];
    
    UIButton *butt3 = (UIButton *)[view viewWithTag:602];
    [butt3 addTarget:self action:@selector(registration:) forControlEvents:(UIControlEventTouchUpInside)];
    
    [self.view addSubview:view];
}


//将时间戳转化为时间
- (NSString *)Date:(NSString *)str{
    NSTimeInterval time=[str doubleValue];
    NSDate *detaildate=[NSDate dateWithTimeIntervalSince1970:time];
    //实例化一个NSDateFormatter对象
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //设定时间格式,这里可以设置成自己需要的格式
    [dateFormatter setDateFormat:@"MM月dd日"];
    NSString *currentDateStr = [dateFormatter stringFromDate: detaildate];
    return currentDateStr;
}

//详情页面15图的点击方法
- (void)TapPic{
    NSLog(@"我是谁?");
}

//点击咨询的方法
- (void)phone:(UIButton *)but1{
    NSString *str = [NSString stringWithFormat:@"呼叫%@",_model.club[@"title"]];
    NSString *str2 = [NSString stringWithFormat:@"(%@)",_model.club[@"phone"]];
    UIAlertView *phone = [[UIAlertView alloc] initWithTitle:str message:str2 delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"呼叫", nil];
    [phone show];
}

//点击收藏
- (void)Collection:(UIButton *)but2{
    LoginViewController *login = [[LoginViewController alloc] init];
    NSLog(@"我登陆去了");
    [self.navigationController pushViewController:login animated:NO];
}

//点击报名
- (void)registration:(UIButton *)but3{
    NSLog(@"报名了");
}








- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
